<!--
 * @Description:
 * @Author: 月间
 * @Date: 2024-11-05 22:37:47
 * @LastEditTime: 2024-11-06 21:28:02
 * @LastEditors:
-->
<script setup lang="tsx">
import { useRoute } from 'vue-router';
import { ref } from 'vue';
import type { Ref } from 'vue';
import { NButton, NPopconfirm, NTag } from 'naive-ui';
import { useBoolean } from '@sa/hooks';
import { fetchGetMenuButton, fetchBatchDeleteMenuButton } from '@/service/api';
import { useAppStore } from '@/store/modules/app';
import { useTable, useTableOperate } from '@/hooks/common/table';
import { $t } from '@/locales';
import { enableStatusRecord } from '@/constants/business';
import ButtonOperateDrawer, { type OperateType } from './modules/button-operate-drawer.vue';

const route = useRoute();

const id: number = Number(route.params.id) ?? 0;
const title: string = route.params.name as string;
const appStore = useAppStore();
const { bool: visible, setTrue: openModal } = useBoolean();
const { columns,
  columnChecks,
  data,
  loading,
  getData,
  getDataByPage,
  mobilePagination,
  searchParams,
  resetSearchParams } = useTable({
    apiFn: fetchGetMenuButton,
    apiParams: {
      mid: id,
      size: 10,
      current: 1
    },
    columns: () => [
      {
        type: 'selection',
        align: 'center',
        width: 48
      },
      {
        key: 'id',
        title: $t('common.index'),
        width: 64,
        align: 'center'
      },
      {
        key: 'code',
        title: '项目编号',
        align: 'center',
        minWidth: 120
      },
      {
        key: 'desc',
        title: '描述',
        align: 'center',
        minWidth: 120
      },
      {
        key: 'status',
        title: '状态',
        align: 'center',
        minWidth: 120,
        render(row) {
          if (row.status === null) {
            return null;
          }

          const tagMap: Record<Api.Common.EnableStatus, NaiveUI.ThemeColor> = {
            1: 'success',
            2: 'warning'
          };

          const label = $t(enableStatusRecord[row.status]);

          return <NTag type={tagMap[row.status]}>{label}</NTag>;
        }
      },
      {
        key: 'operate',
        title: '操作',
        align: 'center',
        minWidth: 50,
        render(row) {
          return <div class="flex-center gap-8px">
            <NButton type="primary" ghost size="small" onClick={() => handleEdit(row)}>
              {$t('common.edit')}
            </NButton>
          </div>
        }
      }
    ]
  }
  )

const { checkedRowKeys, onBatchDeleted, onDeleted } = useTableOperate(data, getData);

  const operateType = ref<OperateType>('add');

async function handleBatchDelete() {
  // request
  console.log(checkedRowKeys.value);
  const { error } = await fetchBatchDeleteMenuButton(checkedRowKeys.value);
  if (error) return;
  onBatchDeleted();
}

const editingData: Ref<Api.SystemManage.MenuButton | null> = ref(null);
function handleEdit(item: Api.SystemManage.MenuButton) {
  operateType.value = 'edit';
  editingData.value = { ...item };

  openModal();
}
function handleAdd() {
  operateType.value = 'add';
  editingData.value = null;

  openModal();
}


</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <NCard :title="title" :bordered="false" size="small" class="sm:flex-1-hidden card-wrapper">
      <template #header-extra>
        <TableHeaderOperation v-model:columns="columnChecks"
          :loading="loading" @refresh="getData">
          <template #prefix>
            <NButton size="small" ghost type="primary" @click="handleAdd">
              <template #icon>
                <icon-ic-round-plus class="text-icon" />
              </template>
              {{ $t('common.add') }}
            </NButton>
            <NPopconfirm @positive-click="handleBatchDelete">
              <template #trigger>
                <NButton size="small" ghost type="error" :disabled="checkedRowKeys.length === 0">
                  <template #icon>
                    <icon-ic-round-delete class="text-icon" />
                  </template>
                  {{ $t('common.batchDelete') }}
                </NButton>
              </template>
              {{ $t('common.confirmDelete') }}
            </NPopconfirm>
          </template>
        </TableHeaderOperation>
      </template>
      <NDataTable
        v-model:checked-row-keys="checkedRowKeys"
        :columns="columns"
        :data="data"
        size="small"
        :flex-height="!appStore.isMobile"
        :scroll-x="702"
        :loading="loading"
        remote
        :row-key="row => row.id"
        :pagination="mobilePagination"
        class="sm:h-full"
        />
      <ButtonOperateDrawer v-model:visible="visible" :operate-type="operateType" :row-data="editingData" :mid="id"
        @submitted="getDataByPage" />
    </NCard>
  </div>
</template>

<style scoped></style>
